package com.ruoyi.system.outto.goods.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttrTemplateMapper;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttributeMapper;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoImgMapper;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.interAttr.domain.InterAttr;
import com.ruoyi.system.interAttr.mapper.InterAttrMapper;
import com.ruoyi.system.model.domain.GameAttrTemplate;
import com.ruoyi.system.model.domain.GameAttribute;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.dto.GoodsInfoAndAttrValueDto;
import com.ruoyi.system.outto.goods.TANXINZHUAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lz
 * @version 1.0
 * @date 2022/8/16 22:58
 */
@Slf4j
@Service
public class TANXINZHUApiImpl implements TANXINZHUAPI {

    @Resource
    InterAttrMapper interAttrMapper;
    @Resource
    private GameAttrTemplateMapper gameAttrTemplateMapper;
    @Resource
    private GameAttributeMapper gameAttributeMapper;
    @Resource
    private ArtRecMapper artRecMapper;
    @Resource
    private GoodsInfoImgMapper goodsInfoImgMapper;
    private static final String HEX_CHARS = "0123456789abcdef";

    @Override
    public AjaxResult addGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, salePartner);

        log.info("寻宝网添加接口请求参数={}", JSONObject.toJSONString(params));
        Map<String, Object> header = new HashMap<>();
        String result = HttpUtils.doPostForm("http://www.tanxinzhu.com/openApi", params, header);
        log.info("寻宝网添加接口返回参数={}", result);
        JSONObject resultJson = JSONObject.parseObject(result);
        if (!"200".equals(resultJson.getString("code"))) {
            resultMap.put("code", 500);
        } else {
            resultMap.put("code", 200);
            resultMap.put("outId", resultJson.getString("commId"));

        }
        resultMap.put("req", JSONObject.toJSONString(params));
        resultMap.put("res", result);
        resultMap.put("msg", resultJson);

        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult editGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {

        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, salePartner);

        log.info("寻宝网编辑接口请求参数={}", JSONObject.toJSONString(params));
        Map<String, Object> header = new HashMap<>();
        String result = HttpUtils.doPostForm("http://www.tanxinzhu.com/openApi", params, header);
        log.info("寻宝网编辑接口返回参数={}", result);
        JSONObject resultJson = JSONObject.parseObject(result);
        if (!"200".equals(resultJson.getString("code"))) {
            resultMap.put("code", 500);
        } else {
            resultMap.put("code", 200);
        }
        resultMap.put("req", JSONObject.toJSONString(params));
        resultMap.put("res", result);
        resultMap.put("msg", resultJson);

        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult delGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("method", "abolishCommodity");
        params.put("productId", goodsInfoAndAttrValueDto.getId());
        params.put("appid", salePartner.getOther());
        String sign_source = "appKey=" + salePartner.getSaleKey() + "&appid=" + salePartner.getOther() + "&productId=" + params.get("productId");
        String sign_md5 = null;
        try {
            sign_md5 = md5Hex(sign_source.getBytes("UTF-8")).toUpperCase();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        params.put("sign", sign_md5);
        log.info("寻宝网删除接口请求参数={}", JSONObject.toJSONString(params));
        Map<String, Object> header = new HashMap<>();
        String result = HttpUtils.doPostForm("http://www.tanxinzhu.com/openApi", params, header);
        log.info("寻宝网删除接口返回参数={}", result);
        JSONObject resultJson = JSONObject.parseObject(result);
        if (!"200".equals(resultJson.getString("code"))) {
            resultMap.put("code", 500);
        } else {
            resultMap.put("code", 200);
        }
        resultMap.put("req", JSONObject.toJSONString(params));
        resultMap.put("res", result);
        resultMap.put("msg", resultJson);

        return AjaxResult.success(resultMap);
    }


    private Map<String, Object> params(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        Map<String, Object> paramss = new TreeMap<>();
        paramss.put("method", "addCommodity");
        paramss.put("password", String.valueOf(System.currentTimeMillis()));
        paramss.put("bindPhone", "1"+String.valueOf(System.currentTimeMillis()).substring(3));
        String accountNumber = goodsInfoAndAttrValueDto.getAccountNumber();
        paramss.put("account", StringUtils.isNotEmpty(accountNumber) ? accountNumber : "");
        paramss.put("accountbinding", "1");//帐号安全绑定 1无绑定 2只绑定身份证 3只绑定手机 4绑定身份证和手机
        paramss.put("accountFromType", "1");//来源1自己注册 2本平台购买 3其他平台购买,入手时间已满6个月 4其他平台购买,入手时间未满6个月
        String aid = "";
        String typeId = "";
        String zoneName = "";
        if (StringUtils.equals("安卓", goodsInfoAndAttrValueDto.getSystemName())) {
            if (StringUtils.equals("王者荣耀", goodsInfoAndAttrValueDto.getGameName())) {
                aid = "60026";
                if (StringUtils.equals("QQ", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2322";
                    zoneName = "手Q1区";
                } else if (StringUtils.equals("微信", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2342";
                    zoneName = "微信1区";
                }
            } else if (StringUtils.equals("和平精英", goodsInfoAndAttrValueDto.getGameName())) {
                aid = "60030";
                if (StringUtils.equals("QQ", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2365";
                    zoneName = "安卓QQ专区";
                } else if (StringUtils.equals("微信", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2366";
                    zoneName = "安卓微信专区";
                }
            }else if (StringUtils.equals("火影忍者", goodsInfoAndAttrValueDto.getGameName())) {
                aid = "60050";
                if (StringUtils.equals("QQ", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2614";
                    zoneName = "1区";
                }
            }else if(StringUtils.equals("枪战王者", goodsInfoAndAttrValueDto.getGameName())){
                aid = "60054";
                zoneName = "通用服务器";
                if (StringUtils.equals("QQ", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2595";
                }else {
                    typeId = "2635";
                }
            }else if(StringUtils.equals("三国杀", goodsInfoAndAttrValueDto.getGameName())){
                aid = "60052";
                zoneName = "安卓服务器";
                if (StringUtils.equals("QQ", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2631";
                }else {
                    typeId = "2632";
                }
            }else if(StringUtils.equals("光遇", goodsInfoAndAttrValueDto.getGameName())){
                aid = "60058";
                zoneName = "全区全服";
                typeId = "2662";
            }else if(StringUtils.equals("英雄联盟(手游)", goodsInfoAndAttrValueDto.getGameName())){
                aid = "60046";
                zoneName = "全服";
                if (StringUtils.equals("QQ", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2364";
                }else {
                    typeId = "2362";
                }
            }else if(StringUtils.equals("使命召唤", goodsInfoAndAttrValueDto.getGameName())){
                aid = "60064";
                zoneName = "通用服务器";
                typeId = "2699";
            }else if(StringUtils.equals("金铲铲之战", goodsInfoAndAttrValueDto.getGameName())){
                aid = "60148";
                zoneName = "通用服务器";
                typeId = "2687";
            }
        } else if (StringUtils.equals("苹果", goodsInfoAndAttrValueDto.getSystemName())) {
            if (StringUtils.equals("王者荣耀", goodsInfoAndAttrValueDto.getGameName())) {
                aid = "60025";
                if (StringUtils.equals("QQ", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2321";
                    zoneName = "手Q1区";
                } else if (StringUtils.equals("微信", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2339";
                    zoneName = "微信1区";
                }
            } else if (StringUtils.equals("和平精英", goodsInfoAndAttrValueDto.getGameName())) {
                aid = "60029";
                if (StringUtils.equals("QQ", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2369";
                    zoneName = "苹果QQ专区";
                } else if (StringUtils.equals("微信", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2371";
                    zoneName = "苹果微信专区";
                }
            } else if (StringUtils.equals("火影忍者", goodsInfoAndAttrValueDto.getGameName())) {
                aid = "60049";
                if (StringUtils.equals("QQ", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2592";
                    zoneName = "1区";
                }
            }else if(StringUtils.equals("枪战王者", goodsInfoAndAttrValueDto.getGameName())){
                aid = "60053";
                zoneName = "通用服务器";
                if (StringUtils.equals("QQ", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2633";
                }else {
                    typeId = "2634";
                }
            }else if(StringUtils.equals("三国杀", goodsInfoAndAttrValueDto.getGameName())){
                aid = "60051";
                zoneName = "移动3.0区";
                typeId = "2630";
            }else if(StringUtils.equals("光遇", goodsInfoAndAttrValueDto.getGameName())){
                aid = "60057";
                zoneName = "全区全服";
                typeId = "2661";
            }else if(StringUtils.equals("英雄联盟(手游)", goodsInfoAndAttrValueDto.getGameName())){
                aid = "60045";
                zoneName = "全服";
                if (StringUtils.equals("QQ", goodsInfoAndAttrValueDto.getCarrierName())) {
                    typeId = "2359";
                }else {
                    typeId = "2363";
                }
            }else if(StringUtils.equals("使命召唤", goodsInfoAndAttrValueDto.getGameName())){
                aid = "60063";
                zoneName = "通用服务器";
                typeId = "2698";
            }else if(StringUtils.equals("金铲铲之战", goodsInfoAndAttrValueDto.getGameName())){
                aid = "60147";
                zoneName = "通用服务器";
                typeId = "2686";
            }
        }
        if(goodsInfoAndAttrValueDto.getGameName().equals("暗区突围")){
            if(goodsInfoAndAttrValueDto.getSystemName().equals("安卓")){
                aid = "60084";
                if(goodsInfoAndAttrValueDto.getCarrierName().equals("QQ")){
                    zoneName = "通用服务器";
                    typeId = "2682";
                }else {
                    zoneName = "通用服务器";
                    typeId = "2786";
                }
            }else {
                aid = "60083";
                if(goodsInfoAndAttrValueDto.getCarrierName().equals("QQ")){
                    zoneName = "通用服务器";
                    typeId = "2681";
                }else {
                    zoneName = "通用服务器";
                    typeId = "2785";
                }
            }
        }
        if(goodsInfoAndAttrValueDto.getGameName().equals("英雄联盟")){
            aid = "60219";
            typeId = "2790";
            zoneName = goodsInfoAndAttrValueDto.getGroupName()+"-"+goodsInfoAndAttrValueDto.getServerName();
        }
        if(goodsInfoAndAttrValueDto.getGameName().equals("三角洲行动")){
            aid = "60231";
            if(goodsInfoAndAttrValueDto.getCarrierName().equals("QQ")) {
                typeId = "2808";
            }else {
                typeId = "2809";
            }
            zoneName = "通用服务器";
        }
        paramss.put("aid", aid);//
        paramss.put("typeId", typeId);//帐号来源
        paramss.put("zoneName", zoneName);//服务器

        paramss.put("appid", salePartner.getOther());//
        paramss.put("desc",  goodsInfoAndAttrValueDto.getDes().replace("+","").replace("✨",""));

        paramss.put("preventAddiction", "2");//1未绑定防沉迷 2绑定防沉迷

        paramss.put("price", goodsInfoAndAttrValueDto.getPrice());
        paramss.put("productId", goodsInfoAndAttrValueDto.getId());//三方商品id
        paramss.put("productName", goodsInfoAndAttrValueDto.getTitle().replace("+",""));//商品标题
        paramss.put("roleLevel", "1");//等级
        paramss.put("roleName", "问客服");//名字

//        GoodsInfoImg goodsInfoImg = new GoodsInfoImg();
//        goodsInfoImg.setGoodsId(goodsInfoAndAttrValueDto.getId());
//        goodsInfoImg.setType(Constants.STR_1);
//        List<GoodsInfoImg> infoImgs = goodsInfoImgMapper.selectGoodsInfoImgList(goodsInfoImg);
//        String pic = infoImgs.stream().map(info -> info.getImgUrl()).collect(Collectors.toList()).stream().collect(Collectors.joining(","));
//        if (StringUtils.isNotEmpty(pic)) {
//            paramss.put("imagePath", pic);
//        } else {
//            paramss.put("imagePath", LCConstant.defaultImg);
//        }
        List<Map<String, Object>> urlArr = goodsInfoAndAttrValueDto.getUrlArr();
        StringBuffer imgs = new StringBuffer();
        if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getTitleImg())){
            imgs.append(goodsInfoAndAttrValueDto.getTitleImg()+",");
        }
        for(Map<String, Object> maps : urlArr){
            String ss = String.valueOf(maps.get("url"));
            String imgStr = ss + ",";
            imgs.append(imgStr);
        }
        String imgUrls = imgs.toString().substring(0, imgs.length() - 1);
        paramss.put("imagePath", imgUrls);
        paramss.put("sign", MD5Hex(paramss,salePartner));//签名
        //属性
        if(goodsInfoAndAttrValueDto.getGameName().equals("王者荣耀")){
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF17())){
                paramss.put("yingdiId", goodsInfoAndAttrValueDto.getF17());
            }
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF16())){
                GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF16()));
                if(gameAttribute1.getFieldName().equals("可二次实名")){
                    paramss.put("secondary", "1");
                }else {
                    paramss.put("secondary", "2");
                }
            }else {
                paramss.put("secondary", "2");
            }
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF2())&&!goodsInfoAndAttrValueDto.getF2().equals("0")){
                GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF2()));
                paramss.put("vipLevel", gameAttribute1.getFieldName());
            }
        }
        if(goodsInfoAndAttrValueDto.getGameName().equals("和平精英")){
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF13())){
                paramss.put("yingdiId", goodsInfoAndAttrValueDto.getF13());
            }
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF10())){
                GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
                if(gameAttribute1.getFieldName().equals("可二次实名")){
                    paramss.put("secondary", "1");
                }else {
                    paramss.put("secondary", "2");
                }
            }else {
                paramss.put("secondary", "2");
            }
        }
        if(goodsInfoAndAttrValueDto.getGameName().equals("枪战王者")){
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF10())){
                GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
                if(gameAttribute1.getFieldName().contains("有修改人脸机会")){
                    paramss.put("secondary", "1");
                }else {
                    paramss.put("secondary", "2");
                }
            }else {
                paramss.put("secondary", "2");
            }
        }

        //属性
        //自动匹配开始
        String substring = salePartner.getSaleName().substring(salePartner.getSaleName().indexOf("_")+1);
        InterAttr interAttr = interAttrMapper.selectInterAttrByConfig(substring, goodsInfoAndAttrValueDto.getGameId());
        JSONObject jsonObject = new JSONObject();
        //开始匹配单选属性
//        builderRadioAttr(goodsInfoAndAttrValueDto, interAttr.getRadioAttr(),jsonObject);
        builderCheckAttr(goodsInfoAndAttrValueDto, interAttr.getCheckAttr(),jsonObject);
        //开始匹配输入框属性
        builderInputAttr(goodsInfoAndAttrValueDto, interAttr.getInputAttr(),jsonObject);
        paramss.put("extInfo", jsonObject.toJSONString());
        return paramss;
    }

    private void builderRadioAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, String radioAttr, JSONObject jsonObject) {
        //========================开始匹配单选属性=====================//
        JSONObject jsonObject1 = JSONObject.parseObject(radioAttr);
        String interRadioReqs = jsonObject1.getString("interRadioReqs");
        log.info(JSONObject.toJSONString(interRadioReqs));
        List<JSONObject> jsonObjects = JSONObject.parseArray(interRadioReqs, JSONObject.class);
        if (org.springframework.util.CollectionUtils.isEmpty(jsonObjects)) {
            throw new GlobalException("接口单选属性暂未配置");
        }
        /**
         * 1、先查询自定义中所有单选的字段
         * 2、再查该账号单选字段中的选项
         * 3、再去接口属性匹配表过滤
         */
        GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
        gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
        gameAttrTemplate.setIsEnable("0");
        gameAttrTemplate.setFieldType("radio");
        List<GameAttrTemplate> gameAttrTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);
        Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();


        List<JSONObject> defObjects = new ArrayList<>();
        defObjects.addAll(jsonObjects);
        String string = "";
        for (JSONObject object : jsonObjects) {
            String InterConfigId = object.getString("InterConfigId");
            for (GameAttrTemplate attrTemplate : gameAttrTemplates) {
                String aavName = attrTemplate.getAavName();
                for (Field declaredField : declaredFields) {
                    string = "";
                    String name = declaredField.getName();
                    if (aavName.equals(name)) {
                        //一级属性
                        //注意 YdConfigId 对应的是 f字段对应的 id
                        if (Objects.equals(object.getLong("YdConfigId"), attrTemplate.getId())) {
                            defObjects.remove(object);//如果匹配上，则去掉
                            String interRadioReqs1 = object.getString("interRadioReqs");
                            List<JSONObject> jsonObjects2 = JSONObject.parseArray(interRadioReqs1, JSONObject.class);
                            declaredField.setAccessible(true);
                            try {
                                //获取选择的值 例如  实名中的可二次
                                Object o = declaredField.get(goodsInfoAndAttrValueDto);
                                String oneValue = String.valueOf(o);
                                List<JSONObject> twoAttr = jsonObjects2.stream().filter(j -> Objects.equals(j.getString("YdConfigId"), oneValue)).collect(Collectors.toList());
                                String defaultValue = object.getString("defaultValue");
                                if (org.springframework.util.CollectionUtils.isEmpty(twoAttr)) {
                                    if (StringUtils.isNotEmpty(defaultValue)) {
                                        jsonObject.put(InterConfigId, defaultValue);
                                    }
                                }
                                if (!org.springframework.util.CollectionUtils.isEmpty(twoAttr)) {
                                    JSONObject jsonObject2 = twoAttr.get(0);
                                    String interConfigId2 = jsonObject2.getString("InterConfigId");
                                    jsonObject.put(InterConfigId, interConfigId2.trim());
                                }
                            } catch (Exception e) {
                                throw new GlobalException("单选项" + attrTemplate.getFieldName() + "匹配错误");
                            }
                        }
                    }
                }
            }
        }
        //开始匹配默认值
        for (JSONObject object : defObjects) {
            String id = object.getString("id");
            interRadioReqs = object.getString("interRadioReqs");
            jsonObjects = JSONObject.parseArray(interRadioReqs, JSONObject.class);
            for (JSONObject objects : jsonObjects) {
                if(StringUtils.isNotEmpty(objects.getString("defaultValue"))){
                    jsonObject.put(id, objects.getString("defaultValue").trim());
                }
            }
        }
        //========================单选属性匹配结束=====================//
    }
    /**
     * 开始匹配输入框属性
     *
     * @param goodsInfoAndAttrValueDto
     * @param
     */
    private void builderInputAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, String inputAttr, JSONObject jsonObject) {
//        JSONArray propertys = Objects.isNull(params.get("property")) ? new JSONArray() : JSONArray.parseArray(params.get("property").toString());
        Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();

        //========================开始匹配输入框=====================//
        JSONObject jsonObject1 = JSONObject.parseObject(inputAttr);
        String inputJson = jsonObject1.getString("inputJson");
        List<JSONObject> jsonObjects1 = JSONObject.parseArray(inputJson, JSONObject.class);
        GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
        gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
        gameAttrTemplate.setIsEnable("0");
        ArrayList types = new ArrayList<>();
        types.add("number");
        types.add("input");
        gameAttrTemplate.setFieldTypes(types);
        List<GameAttrTemplate> inputTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);
        List<JSONObject> defInputObjects = new ArrayList<>();
        defInputObjects.addAll(jsonObjects1);
        String string = "";
        for (JSONObject object : jsonObjects1) {
            for (GameAttrTemplate inputTemplate : inputTemplates) {
                string = "";
                String aavName = inputTemplate.getAavName();
                if (Objects.equals(object.getString("aavName"), inputTemplate.getAavName())) {
                    defInputObjects.remove(object);
                    String aavNameInter = object.getString("interName");
                    String interId = object.getString("interId");
                    for (Field declaredField : declaredFields) {
                        String name = declaredField.getName();
                        if (Objects.equals(aavName, name)) {
                            declaredField.setAccessible(true);
                            Object o = null;
                            try {
                                o = declaredField.get(goodsInfoAndAttrValueDto);
                            } catch (IllegalAccessException e) {
                                throw new GlobalException("3属性参数错误，请匹配后重新配置");
                            }
                            if (Objects.nonNull(o)) {
                                String oneValue = String.valueOf(o);
                                jsonObject.put(object.getString("id"), oneValue);
                            }
                        }
                    }
                }
            }
        }
        //开始匹配默认值
        for (JSONObject object : defInputObjects) {
            String id = object.getString("id");
            String defaultValue = object.getString("defaultValue");
            if (StringUtils.isNotEmpty(defaultValue)) {
                jsonObject.put(id,defaultValue);
            }
        }
    }

    private void builderCheckAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, String checkAttr, JSONObject jsonObject) {
        /**
         * 1、先查询自定义中所有多选的字段
         * 2、再查该账号单选字段中的选项
         * 3、再去接口属性匹配表过滤
         */
        Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();
        JSONObject checkAttrJson = JSONObject.parseObject(checkAttr);
        String interCheckBoxReqs = checkAttrJson.getString("interCheckBoxReqs");
        List<JSONObject> checkJsonList = JSONObject.parseArray(interCheckBoxReqs, JSONObject.class);
        if (org.springframework.util.CollectionUtils.isEmpty(checkAttrJson)) {
            throw new GlobalException("接口多选属性暂未配置");
        }

        GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
        gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
        gameAttrTemplate.setIsEnable("0");
        List<String> types = new ArrayList<>();
        types.add("checkbox");
        types.add("boxradio");
        gameAttrTemplate.setFieldTypes(types);
        List<GameAttrTemplate> checkAttrTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);


        for (JSONObject object : checkJsonList) {
            String pf_ids = "";
            for (GameAttrTemplate attrTemplate : checkAttrTemplates) {

                String aavName = attrTemplate.getAavName();
                for (Field declaredField : declaredFields) {
                    String name = declaredField.getName();
                    if (aavName.equals(name)) {
                        declaredField.setAccessible(true);

//                        List<String> list2 = new ArrayList<>();
//                        for (String s : split2) {
//                            s = s.replace(" ", "");
//                            list2.add(s);
//                        }
                        Object o = null;
                        try {
                            o = declaredField.get(goodsInfoAndAttrValueDto);
                        } catch (IllegalAccessException e) {
                            throw new GlobalException("多选项" + attrTemplate.getFieldName() + "匹配错误");
                        }
                        String oneValue = String.valueOf(o);
                        String interCheckBoxReqsStr = object.getString("interCheckBoxReqs");
                        //开始匹配二级属性
                        List<JSONObject> jsonObjects2 = JSONObject.parseArray(interCheckBoxReqsStr, JSONObject.class);
                        /**
                         * 咱们的f字段  多选项都是用空格 隔开，用,号切割数组，跟匹配的数据匹配、匹配上说明此账号勾选的有此属性
                         */
                        for (JSONObject jsonObject1 : jsonObjects2) {
                            String[] split1 = oneValue.split(",");
                            String ydConfigId1 = jsonObject1.getString("YdConfigId");
                            for (String s : split1) {
                                if (Objects.equals(s, ydConfigId1)) {
                                    String InterConfigId2 = jsonObject1.getString("InterConfigId");
                                    pf_ids+=","+InterConfigId2.trim();
                                }
                            }
                        }
                    }
                }
            }
            if(StringUtils.isNotEmpty(pf_ids)) {
                jsonObject.put(object.getString("id"), pf_ids.substring(1));
            }
        }
    }

    private List<GameAttribute> getGameAttributs(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return new ArrayList<>();
        }
        return gameAttributeMapper.selectGameAttributeListByIds((Long[]) ConvertUtils.convert(ids.split(","),
                Long.class));
    }
    // MD5加密
    private String MD5Hex(Map<String, Object> map, SalePartner salePartner) {
        String sign_source = "account=" + map.get("account") +
                "&accountFromType=" + map.get("accountFromType")
                + "&accountbinding=" + map.get("accountbinding")
                + "&aid=" + map.get("aid")
                + "&appKey=" + salePartner.getSaleKey()
                + "&appid=" + salePartner.getOther()
                + "&desc=" + map.get("desc")
                + "&imagePath=" + map.get("imagePath")
                + "&preventAddiction=" + map.get("preventAddiction")
                + "&price=" + map.get("price")
                + "&productId=" + map.get("productId")
                + "&productName=" + map.get("productName")
                + "&roleLevel=" + map.get("roleLevel")
                + "&roleName=" + map.get("roleName")
                + "&typeId=" + map.get("typeId")
                + "&zoneName=" + map.get("zoneName");
        try {
            return md5Hex(sign_source.getBytes("UTF-8")).toUpperCase();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String md5Hex(byte[] data) {
        try {
            return toHexString(MessageDigest.getInstance("MD5").digest(data));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static String toHexString(byte[] b) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            sb.append(HEX_CHARS.charAt(b[i] >>> 4 & 0x0F));
            sb.append(HEX_CHARS.charAt(b[i] & 0x0F));
        }
        return sb.toString();
    }

}
